package TextBookCode.unit2.unit2_1;

import TextBookCode.unit2.unit2_1.ReflectServiceImpl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectTest {
    public static void main(String[] args) {
        reflect();
        reflectMethod();
    }

    public static Object reflectMethod() {
        Object returnObj = null;
        ReflectServiceImpl target = new ReflectServiceImpl();

        try {
                            //target.getclass()
            Method method = ReflectServiceImpl.class.
                    getMethod("sayHello", String.class);
            returnObj = method.invoke(target, "张三");
        } catch (SecurityException | IllegalAccessException
                | IllegalArgumentException  | InvocationTargetException
                | NoSuchMethodException var4) {
            var4.printStackTrace();
        }

        return returnObj;
    }

    public static Object reflect() {
        ReflectServiceImpl object = null;

        try {
            //反射类
            object = (ReflectServiceImpl)
                    Class.forName("TextBookCode.unit2.unit2_1.ReflectServiceImpl").newInstance();

            //调用方法
            Method method = object.getClass().getMethod("sayHello", String.class);
            method.invoke(object, "张三");

        } catch (SecurityException | ClassNotFoundException
                | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException | InstantiationException
                | NoSuchMethodException var3) {
            var3.printStackTrace();
        }

        return object;
    }
}
